package cn.easybuy.utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
 * 数据库连接工具
 * 1.加载属性集配置文件
 * 2.加载驱动
 * 3.获取连接
 * 4.关闭连接
 * 5.封装update方法
 * 6.封装query方法
 * 7.封装事务控制
 */
public class Utils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static {
        try {
            //创建属性集对象
            Properties properties=new Properties();
            //读取属性集文件
            InputStream input= Utils.class.getClassLoader().getResourceAsStream("database.properties");
            //加载配置信息
            properties.load(input);
            //赋值
            driver=properties.getProperty("mysql.driver");
            url=properties.getProperty("mysql.url");
            username=properties.getProperty("mysql.username");
            password=properties.getProperty("mysql.password");
            //加载驱动
            Class.forName(driver);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取Connection对象的方法
     * @return
     */
    public static Connection getConnection(){
        Connection connection=null;
        try {
            connection= DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭所有连接对象
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
        try{
            if(resultSet != null)resultSet.close();
            if(statement != null)statement.close();
            if(connection != null)connection.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 数据库增删改的方法,自动关闭所有连接对象，无会话功能
     * @param sql
     * @param object
     * @return
     */
    public static int execute(String sql,Object... object){
        int num=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection=getConnection();
            preparedStatement=connection.prepareStatement(sql);
            //参数
            if (object!=null){
                for (int i=0;i<object.length;i++){
                    preparedStatement.setObject(i+1,object[i]);
                }
            }
            num=preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(connection,preparedStatement,null);
        }
        return num;
    }

    /**
     * 增删改的方法，会话完成后才关闭连接
     * @param sql
     * @param connection
     * @param object
     * @return
     */
    public static int execute(String sql,Connection connection,Object... object){
        int num=0;
        PreparedStatement preparedStatement=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            if (object!=null){
                for (int i=0;i<object.length;i++){
                    preparedStatement.setObject(i+1,object[i]);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询数据库表的方法
     * @param sql
     * @param object
     * @return
     */
    public static ResultSet select(String sql,Connection connection,Object... object){
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            if (object!=null){
                for (int i=0;i<object.length;i++){
                    preparedStatement.setObject(i+1,object[i]);
                }
            }
            resultSet=preparedStatement.executeQuery();
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }
}


















